package com.example.administrator.trains;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class admin extends Activity implements View.OnClickListener{
    private LinearLayout linearUser;
    private LinearLayout linearTrain;

    private ImageView imageUser;
    private ImageView imageTrain;

    private TextView textUser;
    private TextView textTrain;

    private Fragment userFragment;
    private Fragment trainFragment;
    //SharedPreferences是一种轻量级的数据存储方式，采用键值对的存储方式
    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        //底部的布局
        linearUser = (LinearLayout) findViewById(R.id.linear_me);
        linearTrain = (LinearLayout) findViewById(R.id.linear_train);

        //底部的图片
        imageUser = (ImageView) findViewById(R.id.image_me);
        imageTrain = (ImageView) findViewById(R.id.image_train);

        //底部的文字
        textUser = (TextView) findViewById(R.id.text_me);
        textTrain = (TextView) findViewById(R.id.text_train);

        //使用默认的首选项文件名
        prefs = PreferenceManager.getDefaultSharedPreferences(admin.this);

        linearUser.setOnClickListener(this);
        linearTrain.setOnClickListener(this);

        // 默认一开始显示第一页
        initFragment(0);
        imageTrain.setImageResource(R.mipmap.train_pressed);
    }

    @Override
    public void onClick(View v) {
        // 在每次点击后将底部所有按钮的颜色改为默认颜色（灰色），然后根据点击着色
        imageUser.setImageResource(R.mipmap.me_normal);
        imageTrain.setImageResource(R.mipmap.train_normal);
        textUser.setTextColor(0xff999999);
        textTrain.setTextColor(0xff999999);

        switch (v.getId()) {
            case R.id.linear_train:
                imageTrain.setImageResource(R.mipmap.train_pressed);
                initFragment(0);
                break;
            case R.id.linear_me:
                imageUser.setImageResource(R.mipmap.me_pressed);
                initFragment(1);
                break;
            default:
                break;
        }
    }

    /**
     * 加载Fragment页面
     * @param index 从0开始计数，0表示第一页
     */
    private void initFragment(int index) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // 隐藏所有Fragment
        if (userFragment != null) {
            transaction.hide(userFragment);
        }
        if (trainFragment != null) {
            transaction.hide(trainFragment);
        }

        switch (index) {
            case 0:
                //车票信息界面
                if (trainFragment == null) {
                    trainFragment = new TicketFragment();
                    transaction.add(R.id.frame_content, trainFragment);
                } else {
                    transaction.show(trainFragment);
                }
                break;
            case 1:
                //用户信息界面
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    transaction.add(R.id.frame_content, userFragment);
                } else {
                    transaction.show(userFragment);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //重新返回键，返回之后直接到开始的登录界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent(admin.this, MainActivity.class);
            startActivity(i);
            this.finish();
        }
        return true;
    }
}
